<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:dxl="http://www.lotus.com/dxl"
    xmlns:session="java:ro.egmar.dojocache.extensions.SessionExtension"
    exclude-result-prefixes="session xs dxl">
    
    <!-- Output options -->
    <xsl:output
        method="html"
        omit-xml-declaration="yes"
        indent="yes"
    />
    
    <!-- Global parameters -->
    <xsl:param name="webDbPath"/>
    <xsl:param name="notesDbPath"/>
    
    <!-- Overriding default templates -->
    <xsl:template match="text()|@*" />
    
    <!-- template for first frame element -->
    <xsl:template name="frameTmpl">
        <xsl:param name="size"/>
        <xsl:param name="orientation"/>
        <xsl:param name="border"/>
        <xsl:param name="position" as="xs:integer"/>
        <xsl:variable name="sizeShare">
            <xsl:value-of select="tokenize($size,' \s*')[$position]"/>    
        </xsl:variable>
        <div dojoType="ContentPane" widgetId="{@name}" sizeShare="{$sizeShare}" style="{if (@scrolling='never') then 'hidden' else 'auto'}">
            <xsl:apply-templates>
                <xsl:with-param name="targetFrame" select="@targetframe"/>
            </xsl:apply-templates>
        </div>
    </xsl:template>
    
    <!-- template for namedelementlink elements -->
    <xsl:template match="dxl:namedelementlink">
        <xsl:param name="targetFrame"/>
        <xsl:choose>
            <xsl:when test="@type = 'form' ">
                <xsl:choose>
                    <xsl:when test="@name">
                        <div dojoType="DominoForm" targetframe="{$targetFrame}" form="{@name}">
                        </div>    
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="db">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='namedelementdb']/formula/text())"/>
                        </xsl:variable>
                        <xsl:variable name="val">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='value']/formula/text())"/>
                        </xsl:variable>
                        <div dojoType="DominoForm" targetframe="{$targetFrame}" form="{$val}" dbReplica="{$db}">
                        </div>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="@type = 'frameset' ">
                <xsl:choose>
                    <xsl:when test="@name">
                        <div dojoType="DominoFrameset" frameset="{@name}">
                        </div>    
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="db">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='namedelementdb']/formula/text())"/>
                        </xsl:variable>
                        <xsl:variable name="val">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='value']/formula/text())"/>
                        </xsl:variable>
                        <div dojoType="DominoFrameset" frameset="{$val}" dbReplica="{$db}">
                        </div>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="@type = 'page' ">
                <xsl:choose>
                    <xsl:when test="@name">
                        <div dojoType="DominoPage" widgetId="{translate(@name,' ','')}" page="{@name}" targetframe="{$targetFrame}">
                        </div>    
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="db">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='namedelementdb']/formula/text())"/>
                        </xsl:variable>
                        <xsl:variable name="val">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='value']/formula/text())"/>
                        </xsl:variable>
                        <div dojoType="DominoPage" targetframe="{$targetFrame}" page="{$val}" dbReplica="{$db}">
                        </div>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="@type = 'view'">
                <xsl:choose>
                    <xsl:when test="@name">
                        <div dojoType="DominoView" view="{@name}">
                        </div>    
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="db">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='namedelementdb']/formula/text())"/>
                        </xsl:variable>
                        <xsl:variable name="val">
                            <xsl:value-of 
                                use-when="function-available('session:evaluate')"
                                select="session:evaluate($notesDbPath,'',dxl:code[event='value']/formula/text())"/>
                        </xsl:variable>
                        <div dojoType="DominoView" view="{$val}" dbReplica="{$db}">
                        </div>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
        </xsl:choose>    
    </xsl:template>
    
    <!-- template for doclink elements -->
    <xsl:template match="dxl:doclink">
         <div dojoType="DominoDocument"></div>
    </xsl:template>
    
    <!-- template for frame elements -->
    <xsl:template match="dxl:viewlink">
        <div dojoType="DominoView"></div>        
    </xsl:template>
    
    <!-- template for frame elements -->
    <xsl:template match="dxl:urllink">
        <xsl:variable name="val">
            <xsl:value-of 
                use-when="function-available('session:evaluate')"
                select="session:evaluate($notesDbPath,'',dxl:code[event='value']/formula/text())"/>
        </xsl:variable>
        <div dojoType="ContentPane" href="{$val}" executeScripts="true">
        </div>      
    </xsl:template>
    
    <!-- template for frameset elements -->
    <xsl:template name="framesetTmpl">
        <xsl:param name="size"/>
        <xsl:param name="orientation"/>
        <xsl:param name="border"/>
        <xsl:param name="position" as="xs:integer"/>
        <xsl:variable name="sizeShare">
            <xsl:value-of select="tokenize($size,' \s*')[$position]"/>    
        </xsl:variable>
        <div dojoType="DominoSplitContainer" 
            orientation="{if (@rows) then 'vertical' else 'horizontal'}"  
            sizerWidth="2" 
            activeSizing="1" sizeShare="{$sizeShare}">
            <xsl:for-each select="dxl:frameset|dxl:frame">
                <xsl:if test="string(node-name(.))='frameset'">
                    <xsl:call-template name="framesetTmpl">
                        <xsl:with-param name="border" select="string-join((@borderwidth,'solid',@bordercolor),' ')" />
                        <xsl:with-param name="size" select="if (../@rows) then ../@rows else ../@columns" />
                        <xsl:with-param name="orientation" select="if (../@rows) then 'rows' else 'columns'" />
                        <xsl:with-param name="position" select="position()" as="xs:integer" />
                    </xsl:call-template> 
                </xsl:if>
                <xsl:if test="string(node-name(.))='frame'">
                    <xsl:call-template name="frameTmpl">
                        <xsl:with-param name="border" select="string-join((@borderwidth,'solid',@bordercolor),' ')" />
                        <xsl:with-param name="size" select="if (../@rows) then ../@rows else ../@columns" />
                        <xsl:with-param name="orientation" select="if (../@rows) then 'rows' else 'columns'" />
                        <xsl:with-param name="position" select="position()" as="xs:integer" />
                    </xsl:call-template>
                </xsl:if>
            </xsl:for-each>
        </div>
    </xsl:template>
    
    <xsl:template match="dxl:frameset">
            <div dojoType="DominoSplitContainer" 
                orientation="{if (@rows) then 'vertical' else 'horizontal'}" 
                sizerWidth="2" 
                activeSizing="1"
                style="height:100%;width:100%;margin:0;padding:0;">
                <xsl:for-each select="dxl:frameset|dxl:frame">
                    <xsl:if test="string(node-name(.))='frameset'">
                        <xsl:call-template name="framesetTmpl">
                            <xsl:with-param name="border" select="string-join((@borderwidth,'solid',@bordercolor),' ')" />
                            <xsl:with-param name="size" select="if (../@rows) then ../@rows else ../@columns" />
                            <xsl:with-param name="orientation" select="if (../@rows) then 'rows' else 'columns'" />
                            <xsl:with-param name="position" select="position()" as="xs:integer" />
                        </xsl:call-template> 
                    </xsl:if>
                    <xsl:if test="string(node-name(.))='frame'">
                        <xsl:call-template name="frameTmpl">
                            <xsl:with-param name="border" select="string-join((@borderwidth,'solid',@bordercolor),' ')" />
                            <xsl:with-param name="size" select="if (../@rows) then ../@rows else ../@columns" />
                            <xsl:with-param name="orientation" select="if (../@rows) then 'rows' else 'columns'" />
                            <xsl:with-param name="position" select="position()" as="xs:integer" />
                        </xsl:call-template>
                    </xsl:if>
                </xsl:for-each>
            </div>
    </xsl:template>
    
    <!-- root template, main entry point -->
    <xsl:template match="/">
        <xsl:apply-templates />    
    </xsl:template>
</xsl:stylesheet>